import androidx.compose.runtime.Composable
import androidx.compose.ui.window.AwtWindow
import androidx.compose.ui.window.FrameWindowScope
import java.awt.FileDialog
import java.io.File

/**
 *  @AUTHOR zhangxiaofan07
 *  @CREATE 2022-03-01 15:04
 */
@Composable
fun FrameWindowScope.FileDialog(
    title: String,
    isLoad: Boolean,
    isMultiple: Boolean,
    onResult: (dirName: String, fileList: List<File>) -> Unit
) = AwtWindow(
    create = {
        object : FileDialog(window, "Choose a file", if (isLoad) LOAD else SAVE) {
            override fun setVisible(value: Boolean) {
                super.setVisible(value)
                if (value) {
                    if (files != null && files.isNotEmpty()) {
                        onResult(directory, files.toList())
                    } else {
                        onResult("", emptyList())
                    }
                }
            }
        }.apply {
            this.title = title
            this.isMultipleMode = isMultiple
        }
    },
    dispose = FileDialog::dispose
)